<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::AttributeMethods::ClassMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::AttributeMethods::ClassMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activerecord/lib/active_record/attribute_methods_rb.html">
                vendor/rails/activerecord/lib/active_record/attribute_methods.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Declare and check for suffixed attribute methods.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001327">attribute_method_suffix</a>&nbsp;&nbsp;
      <a href="#M001335">cache_attribute?</a>&nbsp;&nbsp;
      <a href="#M001333">cache_attributes</a>&nbsp;&nbsp;
      <a href="#M001334">cached_attributes</a>&nbsp;&nbsp;
      <a href="#M001330">define_attribute_methods</a>&nbsp;&nbsp;
      <a href="#M001332">define_read_methods</a>&nbsp;&nbsp;
      <a href="#M001329">generated_methods?</a>&nbsp;&nbsp;
      <a href="#M001331">instance_method_already_implemented?</a>&nbsp;&nbsp;
      <a href="#M001328">match_attribute_method?</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001327" class="method-detail">
        <a name="M001327"></a>

        <div class="method-heading">
          <a href="#M001327" class="method-signature">
          <span class="method-name">attribute_method_suffix</span><span class="method-args">(*suffixes)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Declares a method available for all attributes with the given suffix. Uses
<tt>method_missing</tt> and <tt>respond_to?</tt> to rewrite the method
</p>
<pre>
  #{attr}#{suffix}(*args, &amp;block)
</pre>
<p>
to
</p>
<pre>
  attribute#{suffix}(#{attr}, *args, &amp;block)
</pre>
<p>
An <tt>attribute#{suffix}</tt> instance method must exist and accept at
least the <tt>attr</tt> argument.
</p>
<p>
For example:
</p>
<pre>
  class Person &lt; ActiveRecord::Base
    attribute_method_suffix '_changed?'

    private
      def attribute_changed?(attr)
        ...
      end
  end

  person = Person.find(1)
  person.name_changed?    # =&gt; false
  person.name = 'Hubert'
  person.name_changed?    # =&gt; true
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001327-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001327-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 46</span>
46:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">attribute_method_suffix</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">suffixes</span>)
47:         <span class="ruby-identifier">attribute_method_suffixes</span>.<span class="ruby-identifier">concat</span> <span class="ruby-identifier">suffixes</span>
48:         <span class="ruby-identifier">rebuild_attribute_method_regexp</span>
49:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001335" class="method-detail">
        <a name="M001335"></a>

        <div class="method-heading">
          <a href="#M001335" class="method-signature">
          <span class="method-name">cache_attribute?</span><span class="method-args">(attr_name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns <tt>true</tt> if the provided attribute is being cached.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001335-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001335-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 125</span>
125:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cache_attribute?</span>(<span class="ruby-identifier">attr_name</span>)
126:         <span class="ruby-identifier">cached_attributes</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">attr_name</span>)
127:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001333" class="method-detail">
        <a name="M001333"></a>

        <div class="method-heading">
          <a href="#M001333" class="method-signature">
          <span class="method-name">cache_attributes</span><span class="method-args">(*attribute_names)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<tt><a href="ClassMethods.html#M001333">cache_attributes</a></tt> allows
you to declare which converted attribute values should be cached. Usually
caching only pays off for attributes with expensive conversion methods,
like time related columns (e.g. <tt>created_at</tt>, <tt>updated_at</tt>).
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001333-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001333-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 113</span>
113:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cache_attributes</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">attribute_names</span>)
114:         <span class="ruby-identifier">attribute_names</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span> <span class="ruby-identifier">cached_attributes</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>}
115:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001334" class="method-detail">
        <a name="M001334"></a>

        <div class="method-heading">
          <a href="#M001334" class="method-signature">
          <span class="method-name">cached_attributes</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the attributes which are cached. By default time related columns
with datatype <tt>:datetime, :timestamp, :time, :date</tt> are cached.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001334-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001334-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 119</span>
119:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cached_attributes</span>
120:         <span class="ruby-ivar">@cached_attributes</span> <span class="ruby-operator">||=</span>
121:           <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">select</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">attribute_types_cached_by_default</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">c</span>.<span class="ruby-identifier">type</span>)}.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">:name</span>).<span class="ruby-identifier">to_set</span>
122:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001330" class="method-detail">
        <a name="M001330"></a>

        <div class="method-heading">
          <a href="#M001330" class="method-signature">
          <span class="method-name">define_attribute_methods</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generates all the attribute related methods for columns in the database
accessors, mutators and query methods.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001330-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001330-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 69</span>
69:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">define_attribute_methods</span>
70:         <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">generated_methods?</span>
71:         <span class="ruby-identifier">columns_hash</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span>, <span class="ruby-identifier">column</span><span class="ruby-operator">|</span>
72:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-identifier">name</span>)
73:             <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">serialized_attributes</span>[<span class="ruby-identifier">name</span>]
74:               <span class="ruby-identifier">define_read_method_for_serialized_attribute</span>(<span class="ruby-identifier">name</span>)
75:             <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">create_time_zone_conversion_attribute?</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">column</span>)
76:               <span class="ruby-identifier">define_read_method_for_time_zone_conversion</span>(<span class="ruby-identifier">name</span>)
77:             <span class="ruby-keyword kw">else</span>
78:               <span class="ruby-identifier">define_read_method</span>(<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_sym</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">column</span>)
79:             <span class="ruby-keyword kw">end</span>
80:           <span class="ruby-keyword kw">end</span>
81: 
82:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-node">&quot;#{name}=&quot;</span>)
83:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">create_time_zone_conversion_attribute?</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">column</span>)
84:               <span class="ruby-identifier">define_write_method_for_time_zone_conversion</span>(<span class="ruby-identifier">name</span>)
85:             <span class="ruby-keyword kw">else</span>  
86:               <span class="ruby-identifier">define_write_method</span>(<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_sym</span>)
87:             <span class="ruby-keyword kw">end</span>
88:           <span class="ruby-keyword kw">end</span>
89: 
90:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-node">&quot;#{name}?&quot;</span>)
91:             <span class="ruby-identifier">define_question_method</span>(<span class="ruby-identifier">name</span>)
92:           <span class="ruby-keyword kw">end</span>
93:         <span class="ruby-keyword kw">end</span>
94:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001332" class="method-detail">
        <a name="M001332"></a>

        <div class="method-heading">
          <span class="method-name">define_read_methods</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="ClassMethods.html#M001330">define_attribute_methods</a>
</p>
        </div>
      </div>

      <div id="method-M001329" class="method-detail">
        <a name="M001329"></a>

        <div class="method-heading">
          <a href="#M001329" class="method-signature">
          <span class="method-name">generated_methods?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001329-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001329-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 63</span>
63:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generated_methods?</span>
64:         <span class="ruby-operator">!</span><span class="ruby-identifier">generated_methods</span>.<span class="ruby-identifier">empty?</span>
65:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001331" class="method-detail">
        <a name="M001331"></a>

        <div class="method-heading">
          <a href="#M001331" class="method-signature">
          <span class="method-name">instance_method_already_implemented?</span><span class="method-args">(method_name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Checks whether the method is defined in the model or any of its subclasses
that also derive from Active Record. Raises <a
href="../DangerousAttributeError.html">DangerousAttributeError</a> if the
method is defined by Active Record though.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001331-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001331-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 99</span>
 99:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">instance_method_already_implemented?</span>(<span class="ruby-identifier">method_name</span>)
100:         <span class="ruby-identifier">method_name</span> = <span class="ruby-identifier">method_name</span>.<span class="ruby-identifier">to_s</span>
101:         <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">method_name</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/^id(=$|\?$|$)/</span>
102:         <span class="ruby-ivar">@_defined_class_methods</span>         <span class="ruby-operator">||=</span> <span class="ruby-identifier">ancestors</span>.<span class="ruby-identifier">first</span>(<span class="ruby-identifier">ancestors</span>.<span class="ruby-identifier">index</span>(<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>)).<span class="ruby-identifier">sum</span>([]) { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span><span class="ruby-operator">|</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">public_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">private_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">protected_instance_methods</span>(<span class="ruby-keyword kw">false</span>) }.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">:to_s</span>).<span class="ruby-identifier">to_set</span>
103:         <span class="ruby-ivar">@@_defined_activerecord_methods</span> <span class="ruby-operator">||=</span> (<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">public_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">private_instance_methods</span>(<span class="ruby-keyword kw">false</span>) <span class="ruby-operator">|</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">protected_instance_methods</span>(<span class="ruby-keyword kw">false</span>)).<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">:to_s</span>).<span class="ruby-identifier">to_set</span>
104:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">DangerousAttributeError</span>, <span class="ruby-node">&quot;#{method_name} is defined by ActiveRecord&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@_defined_activerecord_methods</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">method_name</span>)
105:         <span class="ruby-ivar">@_defined_class_methods</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">method_name</span>)
106:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001328" class="method-detail">
        <a name="M001328"></a>

        <div class="method-heading">
          <a href="#M001328" class="method-signature">
          <span class="method-name">match_attribute_method?</span><span class="method-args">(method_name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns MatchData if method_name is an attribute method.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001328-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001328-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 52</span>
52:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">match_attribute_method?</span>(<span class="ruby-identifier">method_name</span>)
53:         <span class="ruby-identifier">rebuild_attribute_method_regexp</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-keyword kw">defined?</span>(<span class="ruby-ivar">@@attribute_method_regexp</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@@attribute_method_regexp</span>
54:         <span class="ruby-ivar">@@attribute_method_regexp</span>.<span class="ruby-identifier">match</span>(<span class="ruby-identifier">method_name</span>)
55:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>